package com.gupt.weekly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gupt.weekly.domain.Report;
import com.gupt.weekly.domain.ReportContent;
import com.gupt.weekly.dto.ReportAndContents;
import com.gupt.weekly.service.ReportContentService;
import com.gupt.weekly.service.ReportService;
import com.gupt.weekly.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author linzizun
 * @version 1.0
 * @date 2021/03/27 11:13
 */
@Slf4j
@RestController
@Api(tags = "周报模块接口")
@RequestMapping("/report")
public class ReportController {
    @Resource
    ReportService reportService;

    @Resource
    ReportContentService reportContentService;

    @ApiOperation(value = "创建周报", notes = "创建周报内容")
    @PostMapping(value = "/createReport")
    public CommonResult createReport(@Valid @RequestBody ReportAndContents reportAndContents) {
        Report report = reportAndContents.getReport();
        report.setId(null);
        report.setSubmit_time(null);
        report.setHas_submit(0);
        int result = reportService.insert(report);
        List<ReportContent> reportContentList = reportAndContents.getReportContentList();
        result = reportContentService.saveBatch(reportContentList) ? result : 0;
        log.info("*****插入结果：" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation(value = "查询周报", notes = "根据id查询周报")
    @GetMapping(value = "/getReportById")
    public CommonResult getReportById(String id) {
        Report report = reportService.selectById(id);
        List<ReportContent> reportContentList = reportContentService
                .lambdaQuery()
                .eq(ReportContent::getReport_id, report.getId())
                .list();
        ReportAndContents reportAndContents = new ReportAndContents(report, reportContentList);
        if (null != report) {
            return new CommonResult(200, "查询数据库成功", reportAndContents);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "获取周报列表", notes = "获取周报内容列表")
    @GetMapping(value = "/getReportList")
    public CommonResult getReportList(@RequestBody Report report) {
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", report.getUser_id())
                .or().eq("plan_id", report.getPlan_id());
        List<Report> reportList = reportService.list(queryWrapper);
        if (reportList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", reportList);
        } else {
            return new CommonResult(444, "数据库无相关记录");
        }
    }

    @ApiOperation(value = "更改周报主体", notes = "根据id更改周报主体")
    @PostMapping(value = "/updateReportById")
    public CommonResult updateReportById(@Valid @RequestBody Report report) {
        boolean result = reportService.updateById(report);
        if (result) {
            return new CommonResult(200, "更新数据库成功");
        } else {
            return new CommonResult(444, "更新数据库失败");
        }
    }

    @ApiOperation(value = "发布周报", notes = "根据id发布填写完的周报内容")
    @GetMapping(value = "/submitReportById")
    public CommonResult submitReportById(String id) {
        Report report = new Report();
        report.setId(id);
        report.setSubmit_time(new Date());
        report.setHas_submit(1);
        boolean result = reportService.updateById(report);
        if (result) {
            return new CommonResult(200, "更新数据库成功");
        } else {
            return new CommonResult(444, "更新数据库失败");
        }
    }

    @ApiOperation(value = "创建周报事项", notes = "创建周报事项内容")
    @PostMapping(value = "/createReportContent")
    public CommonResult createReportContent(@Valid @RequestBody ReportContent reportContent) {
        reportContent.setId(null);
        boolean result = reportContentService.save(reportContent);
        if (result) {
            return new CommonResult(200, "插入数据库成功");
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation(value = "更改周报事项", notes = "根据id更改周报事项内容")
    @PostMapping(value = "/updateReportContentById")
    public CommonResult updateReportContentById(@Valid @RequestBody ReportContent reportContent) {
        boolean result = reportContentService.updateById(reportContent);
        if (result) {
            return new CommonResult(200, "更新数据库成功");
        } else {
            return new CommonResult(444, "更新数据库失败");
        }
    }

    @ApiOperation(value = "删除周报事项", notes = "根据id删除周报事项内容")
    @DeleteMapping(value = "/deleteReportContentById")
    public CommonResult deleteReportContentById(String id) {
        boolean result = reportContentService.removeById(id);
        if (result) {
            return new CommonResult(200, "删除数据成功");
        } else {
            return new CommonResult(444, "删除数据失败");
        }
    }
}
